//
//  HNSH_TongZhengBottomVIew.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/27.
//

#import "HNSH_TongZhengBottomVIew.h"

@interface HNSH_TongZhengBottomItemView : UIView

/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation HNSH_TongZhengBottomItemView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tz_line_zhu_bg"]];
    [self addSubview:imgV];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"";
    titleLabel.textColor = [UIColor appWhiteLabelColor];
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 26, 0));
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(17);
    }];
}

@end

@interface HNSH_TongZhengBottomVIew ()

/* scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/* topImgV */
@property (nonatomic, strong) UIImageView *topImgV;
/* leftTitles */
@property (nonatomic, strong) NSMutableArray *leftTitles;

@end

@implementation HNSH_TongZhengBottomVIew

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#101010"];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_TongZhengModel *)model {
    _model = model;
    CGFloat maxValue = 0;
    for (HNSH_TongZhengChartItemModel *item in model.chart_data) {
        if ([item.value doubleValue] > maxValue) {
            maxValue = [item.value doubleValue];
        }
    }
    for (int i = 0; i < self.leftTitles.count; i ++) {
        UILabel *label = self.leftTitles[i];
        if (i == 0) {
            label.text = [NSString stringWithFormat:@"%.3f",maxValue];
        } else if (i == 1) {
            label.text = [NSString stringWithFormat:@"%.3f",maxValue/4*3];
        } else if (i == 2) {
            label.text = [NSString stringWithFormat:@"%.3f",maxValue/4*2];
        } else if (i == 3) {
            label.text = [NSString stringWithFormat:@"%.3f",maxValue/4];
        } else if (i == 5) {
            label.text = @"0";
        }
    }
    
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.scrollView.contentSize = CGSizeMake(17 + 80*model.chart_data.count, 0);
    CGFloat height = 240.0;
    for (int i = 0; i < model.chart_data.count; i ++) {
        HNSH_TongZhengChartItemModel *item = model.chart_data[i];
        CGFloat ratio = [item.value doubleValue]/maxValue;
        HNSH_TongZhengBottomItemView *view = [[HNSH_TongZhengBottomItemView alloc]initWithFrame:CGRectMake(17 + 80*i, 272-height*ratio, 40, height*ratio+26)];
        view.titleLabel.text = item.date;
        [self.scrollView addSubview:view];
    }
}

- (void)configUI {
    UIImageView *topImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tz_bottom_icon_2"]];
    topImgV.frame = CGRectMake(12, 24, kScreenWidth-24, 28);
    [self addSubview:topImgV];
    self.topImgV = topImgV;
    
    self.leftTitles = [NSMutableArray arrayWithCapacity:5];
    for (int i = 0; i < 5; i ++) {
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.topImgV.origin.y+self.topImgV.height+24+60*i, 36, 17)];
        titleLabel.text = @"0";
        titleLabel.textColor = [UIColor appWhiteLabelColor];
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:titleLabel];
        [self.leftTitles addObject:titleLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(40, self.topImgV.origin.y+self.topImgV.height+32+60*i, kScreenWidth-40, 1)];
        [self addSubview:line];
        if (i < 4) {
            [self drawLineOfDashByCAShapeLayer:line lineLength:2 lineSpacing:2 lineColor:[UIColor appWhiteLabelColor] lineDirection:YES];
        } else {
            line.backgroundColor = [UIColor appWhiteLabelColor];
        }
    }
    
    [self addSubview:self.scrollView];
    
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40, self.topImgV.origin.y+self.topImgV.height, kScreenWidth-40, 330)];
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

/**
 *  通过 CAShapeLayer 方式绘制虚线
 *
 *  param lineView:       需要绘制成虚线的view
 *  param lineLength:     虚线的宽度
 *  param lineSpacing:    虚线的间距
 *  param lineColor:      虚线的颜色
 *  param lineDirection   虚线的方向  YES 为水平方向， NO 为垂直方向
 **/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    [shapeLayer setBounds:lineView.bounds];
    
    if (isHorizonal) {
        
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
        
    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }
    
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {

        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    
    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }

    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
